<template>
	<div class="system-dept-container">
		<el-card shadow="hover">
			<div class="system-dept-search mb15">
        <el-form :inline="true">
          <el-form-item label="名称">
            <el-input size="default" v-model="tableData.param.name" placeholder="请输入名称" class="w-50 m-2" clearable/>
          </el-form-item>
		  <el-form-item label="状态">
            <el-select size="default" placeholder="请选择状态" class="w-50 m-2" v-model="tableData.param.status" clearable>
              <el-option label="启用"  value="1" />
              <el-option label="禁用"  value="0" />
            </el-select>
          </el-form-item>
 
				  <el-form-item>
            <el-button size="default" type="primary" class="ml10" @click="initList">
              <el-icon>
                <ele-Search />
              </el-icon>
              查询
            </el-button>
            <el-button size="default" type="success" class="ml10" @click="onOpenAdd">
              <el-icon>
                <ele-FolderAdd />
              </el-icon>
              新增
            </el-button>
          </el-form-item>
        </el-form>
      </div>
			<el-table
				:data="tableData.data"
				style="width: 100%"
				row-key="id"
				default-expand-all
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
			>
				<el-table-column prop="name" label="名称" show-overflow-tooltip> </el-table-column>
				<el-table-column prop="contact" label="联系方式" show-overflow-tooltip> </el-table-column>
				<el-table-column prop="status" label="状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status===1">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="money" label="余额" show-overflow-tooltip> </el-table-column>
				<el-table-column prop="join_employee.name" label="入会店员" show-overflow-tooltip> </el-table-column>
				<el-table-column prop="remark" label="备注" show-overflow-tooltip> </el-table-column>

				
				<el-table-column label="操作" width="200">
					<template #default="scope">
						<el-button size="small" text type="danger" @click="onOpenRecharge(scope.row)">充值</el-button>
						<el-button size="small" text type="warning" @click="onOpenMember(scope.row)">记录</el-button>
						<el-button size="small" text type="primary" @click="onOpenEdit(scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<pagination
          v-show="tableData.total>0"
          :total="tableData.total"
          v-model:page="tableData.param.pageNum"
          v-model:limit="tableData.param.pageSize"
          @pagination="initList"
      />
		<Edit ref="editRef" @list="initList"/>
		<Recharge ref="rechargeRef" @list="initList"/>

		<Member ref="memberRef"/>

		
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted, defineComponent,getCurrentInstance } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import Edit from '/@/views/zmm/member/component/edit.vue';
import Recharge from '/@/views/zmm/member/component/recharge.vue';
import Member from '/@/views/zmm/member/component/money_log.vue';
import {destory, getList} from "/@/api/zmm/member";

// 定义接口来定义对象的类型
interface TableDataRow {
  id:number;
  name: string;
	contact: string;
	remark: string;
	status: number;
	children?:TableDataRow[];
}
interface TableDataState {
	tableData: {
		data: Array<TableDataRow>;
		loading: boolean;
		memberListShowVisiable:boolean;
		total: number;

		param: {
			pageNum: number;
			pageSize: number;
      name:string;
	  status:string;
		};
	};
}

export default defineComponent({
	name: 'zmm_shop',
	components: { Edit, Recharge, Member },
	setup() {
    const {proxy} = getCurrentInstance() as any;
		const editRef = ref();
		const rechargeRef = ref();
		const memberRef = ref();
		const state = reactive<TableDataState>({
			tableData: {
				data: [],
				loading: false,
				memberListShowVisiable:false,
				total: 0,
				param: {
					pageNum: 1,
					pageSize: 10,
          name:'',
		  status: '',
				},
			},
		});
		// 初始化表格数据
		const initTableData = () => {
			initList();
		};
    const initList = ()=>{
      getList(state.tableData.param).then((res:any)=>{
        state.tableData.data = proxy.handleTree(res.data.list??[], "id","up_id");
		state.tableData.total = res.data.total;

      });
    };
	const onOpenMember = (row: TableDataRow) => {
		// state.tableData.memberListShowVisiable = true;
		memberRef.value.openDialog(row.id);
		console.log(memberRef.value)
		// memberRef.value.params.member_id = row.id


		};
	const onOpenRecharge = (row: TableDataRow) => {
			rechargeRef.value.openDialog(row);
		};
		// 打开新增菜单弹窗
		const onOpenAdd = (row?: TableDataRow) => {
      editRef.value.openDialog(row?.id);
		};
		// 打开编辑菜单弹窗
		const onOpenEdit = (row: TableDataRow) => {
			editRef.value.openDialog({...row});
		};
		// 删除当前行
		const onTabelRowDel = (row: TableDataRow) => {
			ElMessageBox.confirm(`此操作将永久删除：${row.name}, 是否继续?`, '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
          destory(row.id).then(()=>{
					  ElMessage.success('删除成功');
            initList();
          })
				})
				.catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			editRef,
			rechargeRef,
			memberRef,
      initList,
	  onOpenMember,
			onOpenAdd,
			onOpenEdit,
			onTabelRowDel,
			onOpenRecharge,
			...toRefs(state),
		};
	},
});
</script>
